package com.cms.common.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cms.framework.web.WebConstants;
import com.cms.security.LoginInfo;
import com.cms.security.WebSecurityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class BaseController {
	protected static Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";
    protected static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8";
    protected static final String DEFAULT_CHARSET_ENCODING = "UTF-8";

    protected static final String RESULT = "result";
    protected static final String PAGE = "page";

    /**
     * 标志
     */
    protected static final String FLAG = "flag";

    /**
     * 获取Session中的MemberAccountInfo对象
     * 
     * @return
    //	 */
    // protected MemberAccountInfo getMemberAccountInfo() {
    // return MemberSessionUtility.getMemberAccountInfoFromSession();
    // }
    //


    protected LoginInfo getLoginUserInfo() {
        return WebSecurityUtils.getLoginUserInfoFromSession();
    }

    /**
     * 设置重定向标识
     * 
     * @param session
     */
    protected void setRefererLock(HttpSession session) {
        session.setAttribute(WebConstants.HTTP_REFERER_LOCK, true);
    }

    /**
     * 写出JSON数据，默认关闭PW对象
     * 
     * @param pw
     * @param o
     */
    protected void writeObjectToJson(PrintWriter pw, Object o) {
        try {
            pw.write(new ObjectMapper().writeValueAsString(o));
        } catch (IOException e) {
            logger.error("转换JSON数据异常：", e);
            e.printStackTrace();
        } finally {
            flushPrintWriter(pw);
        }
    }

    /**
     * 获取PW对象
     * <p>
     * 设置PW返回值类型编码先调用
     * </p>
     * 
     * @param response
     * @return
     */
    protected PrintWriter getPrintWriter(HttpServletResponse response) {
        return getPrintWriterCT(response, null, null);
    }

    /**
     * 获取支持设置PW字符集和内容类型的PW对象
     * 
     * @param ct
     *            setContentType
     * 
     * @param ce
     *            setCharacterEncoding
     */
    protected PrintWriter getPrintWriterCT(HttpServletResponse response, String ct, String ce) {
        response.setContentType(ct == null ? DEFAULT_CONTENT_TYPE : ct);
        response.setCharacterEncoding(ce == null ? DEFAULT_CHARSET_ENCODING : ce);

        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            logger.error("获取PrintWriter失败：", e);
            e.printStackTrace();
        }
        return pw;
    }

    /**
     * 刷新PW缓存并关闭
     * 
     * @param pw
     */
    protected void flushPrintWriter(PrintWriter pw) {
        pw.flush();
        pw.close();
    }

    public String getAppPath(HttpServletRequest request, HttpServletResponse response) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                        + request.getServletPath();
    }
}
